<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\EventRecord;
use App\Models\Event;
use Faker\Generator;

class EventRecordSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 防止内存耗尽的异常
        ini_set('memory_limit', -1);

        $events = Event::all()->pluck('label')->toArray();
        // 获取 Faker 实例
        $faker = app(Generator::class);
        foreach ($events as $event) {
            for ($i = 0; $i < 100; $i++) {
                $rows = EventRecord::factory()->times(1000)->make()->each(function ($row, $index) use ($faker, $event) {
                    $now = $faker->dateTimeBetween('-30 days');
                    $row->event = $event;
                    $row->created_at = $now;
                    $row->updated_at = $now;
                });
                EventRecord::insert($rows->toArray());
                print("第 $i 批数据插入完成 \n");
            }
        }
        print("插入完成 \n");
    }
}
